package com.ev.asl.service.impl;

import com.ev.asl.constant.AslConstant;
import com.ev.asl.dao.AslBarCodeDao;
import com.ev.asl.domain.AslBarCodeDO;
import com.ev.asl.domain.AslBarCodeTrayDO;
import com.ev.asl.dto.AslBarCodeParam;
import com.ev.asl.dto.AslBarCodeTrayParam;
import com.ev.asl.service.AslBarCodeService;
import com.ev.asl.vo.AslBarCodeVO;
import com.ev.framework.utils.HttpClientUtils;
import com.ev.framework.utils.R;
import com.ev.k3cloudBusiness.vo.K3DeeSyncVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 姚家乐
 * @since 2021/7/6 17:18
 */
@Slf4j
@Service
public class AslBarCodeServiceImpl implements AslBarCodeService {

    @Autowired
    private AslBarCodeDao dao;

    private static final String URL = "";

    @Override
    public List<AslBarCodeDO> listJob() {
        return dao.listJob();
    }

    @Override
    public AslBarCodeTrayDO getTrayById(Long id) {
        return dao.getTrayById(id);
    }

    @Override
    public Integer countByTaryId(Long trayId) {
        return dao.countByTrayId(trayId);
    }

    @Override
    public R batchRemove(List<Integer> ids) {
        if(dao.batchRemove(ids)>0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    @Async
    public R sync() {
        String resultObj = HttpClientUtils.doGet(URL, null);
        K3DeeSyncVO<List<AslBarCodeVO>> kingdeeSyncVO = new Gson().fromJson(resultObj,new TypeToken<K3DeeSyncVO<List<AslBarCodeVO>>>(){}.getType());

        if(!AslConstant.ZERO.equals(kingdeeSyncVO.getCode()))
        {
            log.info(kingdeeSyncVO.getMsg());
            return  R.error(kingdeeSyncVO.getMsg());
        }

        //同步的远端数据
        List<AslBarCodeVO> resultVO = kingdeeSyncVO.getData();
        //本地的数据
        AslBarCodeParam param = new AslBarCodeParam();
        List<AslBarCodeDO> thisList = dao.list(param);
        //远端数据的序列号（唯一标识）
        List<String> farEndNos = resultVO.stream().map(AslBarCodeVO::getSeriesNumber).collect(Collectors.toList());
        //本地数据的序列号（唯一标识）
        List<String> thisNos = thisList.stream().map(AslBarCodeDO::getSeriesNumber).collect(Collectors.toList());
        //远端有，本地有（唯一标识）
        List<String> updateNos  = farEndNos.stream().filter(thisNos::contains).collect(Collectors.toList());
        //需要新增的(剔除了本地有的)
        farEndNos.removeAll(updateNos);

        if (farEndNos.size() > AslConstant.ZERO){

            List<AslBarCodeVO> insertVO = resultVO.stream().filter(e->farEndNos.contains(e.getSeriesNumber())).collect(Collectors.toList());

            List<AslBarCodeDO> insertDO = new ArrayList<>();
            for (AslBarCodeVO vo : insertVO) {
                AslBarCodeDO codeDO = new AslBarCodeDO();
                codeDO.setStatus(0);
                codeDO.setBagging(0);
                codeDO.setSeriesNumber(vo.getSeriesNumber());
                codeDO.setTrayId(AslConstant.ZERO.longValue());
                insertDO.add(codeDO);
            }
            dao.batchSave(insertDO);
            log.info("批量新增");
        }
        return R.ok();
    }

    @Override
    public AslBarCodeDO get(Long id) {
        return dao.get(id);
    }

    @Override
    public List<AslBarCodeDO> list(AslBarCodeParam param) {
        return dao.list(param);
    }

    @Override
    public int updateStatus(Long trayId) {
        return dao.updateStatus(trayId);
    }

    @Override
    public List<AslBarCodeDO> listByIds(List<Integer> ids) {
        return dao.listByIds(ids);
    }

    @Override
    public Integer count(AslBarCodeParam param) {
        return dao.count(param);
    }

    @Override
    public int update(AslBarCodeDO aslBarCodeDO) {
        return dao.update(aslBarCodeDO);
    }

    @Override
    public int batchSave(List<AslBarCodeDO> list) {
        return dao.batchSave(list);
    }

    @Override
    public int batchUpdate(List<AslBarCodeDO> list) {
        return dao.batchUpdate(list);
    }

    @Override
    public List<AslBarCodeTrayDO> trayList(AslBarCodeTrayParam param) {
        return dao.trayList(param);
    }

    @Override
    public int traySave(AslBarCodeTrayDO trayDO) {
        return dao.traySave(trayDO);
    }

    @Override
    public int batchDeleteTray(AslBarCodeTrayParam param) {
        return dao.batchDeleteTray(param);
    }

    @Override
    public int batchUpdateStatus(AslBarCodeTrayParam param) {
        return dao.batchUpdateStatus(param);
    }

    @Override
    public int batchUpdateTrays(List<AslBarCodeTrayDO> aslBarCodeTrayDOs) {
        return dao.batchUpdateTrays(aslBarCodeTrayDOs);
    }

    @Override
    public List<Map<String, Object>> getCodeByUuid(List<String> uuid) {
        return dao.getCodeByUuid(uuid);
    }

    @Override
    public int save(AslBarCodeDO aslBarCodeDO) {
        return dao.save(aslBarCodeDO);
    }

    /**
     * 查找托盘信息
     * @param trayId 托盘id
     * @param type 1-入库；2-出库
     * @return
     */
    @Override
    public Map<String, Object> getTray(Long trayId,Long type) {
        Map<String, Object> tray = dao.getTray(trayId);
        if (ObjectUtils.isEmpty(tray)){
            return null;
        }
        String batchNos = ObjectUtils.isEmpty(tray.get("batchNos")) ? null : (String) tray.get("batchNos");
        if (ObjectUtils.isEmpty(type) || StringUtils.isBlank(batchNos)){
            return tray;
        }
        HashMap<String, Object> map = new HashMap<>();
        List<String> batchNoList = Arrays.stream(batchNos.split(",")).distinct().collect(Collectors.toList());
        //显示该桶的批次在哪些库位上有
        map.put("materialName",tray.get("materialName"));
        map.put("batchNos",batchNoList);
        List<Map<String, Object>> facility = dao.getFacility(map);
        tray.put("data",facility);
        return tray;
    }

    @Override
    public List<Map<String, Object>> listTrays(Map<String, Object> map) {
        return dao.listTrays(map);
    }

    @Override
    public List<Map<String, Object>> getFacilityTray(Long locationId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("locationId",locationId);
        return dao.getFacilityTray(map);
    }
}
